home *** CD-ROM | disk | FTP | other *** search
- /* Version C */
-
- /*===========================================================\
- | UltraShell (tm) |
- | |
- | ucommand.c |
- | This module permits external commands to obtain |
- | command line arguments and to access UltraShell |
- | files for standard input, output, and error. |
- | |
- | Copyright (C) 1994 by Zack T. Smith |
- | |
- | Permission is granted to freely distribute this |
- | file, so long as the data contained herein is not |
- | altered in any way. |
- \===========================================================*/
-
-
- #include <stdio.h>
- #include <string.h>
- #include <Files.h>
- #include "ucommand.h"
-
-
- FILE *shell_stdin;
- FILE *shell_stdout;
- FILE *shell_stderr;
-
-
- extern errno;
-
-
- static bcopy (char *dest, char *src, unsigned long count)
- {
- while (count--)
- *dest++ = *src++;
- }
-
-
- static char *strdup (char *str)
- {
- char *new;
-
- new = (char*) malloc (strlen (str) + 1);
-
- strcpy (new, str);
-
- return new;
- }
-
-
- static void
- p2c (char *dest, char *src)
- {
- int len = *src;
-
- bcopy (dest, src+1, len);
- dest[len] = 0;
- }
-
-
- static void
- c2p (unsigned char *dest, char *src)
- {
- int len = strlen (src);
-
- if (len>255)
- {
- dest[0] = 0;
- return;
- }
- dest[0] = len;
- bcopy ((char*)(dest+1), src, len);
- }
-
-
- static remove_newlines (char *str)
- {
- char *tmp;
-
- tmp = strchr (str, '\n');
- if (tmp) *tmp = 0;
- tmp = strchr (str, '\r');
- if (tmp) *tmp = 0;
- }
-
-
- static int
- get_tempdir_path (char *return_path)
- {
- FILE *f;
- char path [MAX_PATH_LEN];
- short vol;
- Str31 vol_name;
- long system_folder;
- short oserr;
-
- if (GetVol (vol_name, &vol))
- {
- /* printf ("cannot get volume information\n"); */
- return CANNOT_FIND_TEMP_DIR;
- }
-
- p2c (path, (char*) vol_name);
- strcat (path, ":System Folder:UltraShellTempDirLocation");
-
- f = fopen (path, "r");
- if (!f)
- {
- /* printf ("can't open %s\n", path); */
- return CANNOT_FIND_TEMP_DIR;
- }
-
- fgets (return_path, MAX_PATH_LEN, f);
- fclose(f);
-
- remove_newlines (return_path);
-
- return SUCCESS;
- }
-
-
- static int
- setup_stdio (void)
- {
- FILE *f;
- char path [MAX_PATH_LEN];
- char tmpdir [MAX_PATH_LEN];
-
- shell_stdout = NULL;
- shell_stdin = NULL;
- shell_stderr = NULL;
-
- if (get_tempdir_path (tmpdir))
- return 0;
-
- strcpy (path, tmpdir);
- strcat (path, "stdin");
-
- f = fopen (path, "r");
- if (f)
- {
- shell_stdin = f;
- }
-
- strcpy (path, tmpdir);
- strcat (path, "stdout");
-
- f = fopen (path, "w");
- if (f)
- shell_stdout = f;
-
- strcpy (path, tmpdir);
- strcat (path, "stderr");
-
- f = fopen (path, "w");
- if (f)
- shell_stderr = f;
-
- return 1;
- }
-
-
- void
- shell_exit (short return_value)
- {
- FILE *f;
- char path [MAX_PATH_LEN];
- long dummy;
-
- if (shell_stdin) fclose (shell_stdin);
- if (shell_stdout) fclose (shell_stdout);
- if (shell_stderr) fclose (shell_stderr);
-
- if (get_tempdir_path (path))
- exit (1);
-
- strcat (path, "return_value");
-
- f = fopen (path, "w");
- if (f)
- {
- fprintf (f, "%d\n", return_value);
- fclose (f);
- }
-
- exit (0);
- }
-
-
- long
- ucommand (char **argv, unsigned long max_args)
- {
- FILE *argfile;
- char line [500];
- Str255 cwd;
- unsigned long argc;
- unsigned long i;
- char args_file_path [MAX_PATH_LEN];
-
- if (get_tempdir_path (args_file_path))
- {
- printf ("Can't get the temp dir path\n");
- return 0;
- }
-
- strcat (args_file_path, "args");
-
- argfile = fopen (args_file_path, "r");
- if (!argfile)
- {
- printf ("Can't open args file %s\n", args_file_path);
- return 0;
- }
-
- if (!fgets (line, 499, argfile))
- {
- printf ("Can't read the working directory path from the args file.\n");
- return 0;
- }
-
- remove_newlines (line);
-
- if (strlen(line) > 255)
- {
- printf ("The working directory path string is too long (over 255 chars).\n");
- return 0;
- }
-
- c2p (cwd, line);
-
- if (HSetVol (cwd, 0, 0L))
- {
- printf ("Cannot change to the working directory specified by the shell.\n");
- return 0;
- }
-
- if (!fgets (line, 499, argfile))
- {
- printf ("Can't read the argc line from args file.\n");
- return 0;
- }
-
- if (1 != sscanf (line, "%lu", &argc))
- {
- printf ("Can't read argc value\n");
- return 0;
- }
-
- if (argc > max_args)
- argc = max_args;
-
- for (i = 0; i < argc; i++)
- {
- if (!fgets (line, 499, argfile))
- return 0;
-
- remove_newlines (line);
-
- argv [i] = strdup (line);
- }
-
- fclose (argfile);
-
- if (!setup_stdio ())
- ;
-
- return argc;
- }
-
-
-
-